class TagsController < ApplicationController
  # GET /tags
  # GET /tags.xml
  def index
    @tags = Tag.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @tags }
    end
  end

  # GET /tags/1
  # GET /tags/1.xml
  def show
    @tag = Tag.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @tag }
    end
  end

  # GET /tags/new
  # GET /tags/new.xml
  def new
    @tag = Tag.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @tag }
    end
  end

  # GET /tags/1/edit
  def edit
    @tag = Tag.find(params[:id])
  end

  # POST /tags
  # POST /tags.xml
  def create
    @tag = Tag.new(params[:tag])

    respond_to do |format|
      if @tag.save
        flash[:notice] = 'Tag was successfully created.'
        format.html { redirect_to(@tag) }
        format.xml  { render :xml => @tag, :status => :created, :location => @tag }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @tag.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /tags/1
  # PUT /tags/1.xml
  def update
    @tag = Tag.find(params[:id])

    respond_to do |format|
      if @tag.update_attributes(params[:tag])
        flash[:notice] = 'Tag was successfully updated.'
        format.html { redirect_to(@tag) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @tag.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /tags/1
  # DELETE /tags/1.xml
  def destroy
    @tag = Tag.find(params[:id])
    @tag.destroy

    respond_to do |format|
      format.html { redirect_to(tags_url) }
      format.xml  { head :ok }
    end
  end


  def addtag
    if params["tag"]
      if session[:status]=="matched"
        next_round
      else
        @tag = Tag.find_by_name(params["tag"])
        if @tag==nil
          @tag = Tag.new
          @tag.name = params["tag"]
          @tag.save
        end
        @tag.add_to_picture(session[:picture_id])
        @partner_session = CGI::Session::ActiveRecordStore::Session.find_by_session_id(session[:partner_session_id]);
        session[:tags] << params["tag"]
        puts @partner_session.data[:user_id]
        @partner_session.data[:partner_count] = session[:tags].length
        if Tag.is_tag_in_bag(params["tag"], @partner_session.data[:tags])
          session[:matched] = params["tag"]
          session[:status] = "matched"
          @partner_session.data[:matched] = params["tag"]
          @partner_session.data[:status] = "matched"
          next_round
        else
          puts "not match..continue"
        end
        @partner_session.save
      end
    end
  end

  def next_round
    @game = Game.find(session[:game_id])
    @round = @game.get_next_round
    session[:round_id] = @round.id
    session[:picture_id] = @round.picture.id
    @partner_session.data[:round_id] = @round.id
    @partner_session.data[:picture_id] = @round.picture.id
    @picture = @round.picture
  end


end
